package com.cos.demo2.factory;

import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author: COS
 * @time: 2021/5/17 10:44
 * @description:
 */
public class ConnectionFactoryBean implements FactoryBean<Connection> {
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/17 10:45
     * @description: 用于返回创建的复杂对象的方法,后面spring容器会自动调用此方法获取对象实例
     */
    @Override
    public Connection getObject() throws Exception {
        //类加载
        Class.forName("com.mysql.jdbc.Driver");
        //创建链接
        Connection cos = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp?useUnicode=true&characterEncoding=utf8", "cos", "123456");
        return cos;
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/17 10:45
     * @description:  这个工厂类要返回的对象的数据类型
     */
    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/17 10:52
     * @description: 设置创建出来的对象是否单例
     */
    @Override
    public boolean isSingleton() {
        return false;
    }
}
